package com.yphone.ui.widget;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout.LayoutParams;
import android.widget.Scroller;

import com.yphone.tools.NiceUtil;

public class StickyScrollView extends StretchScrollView {
    public static final int ANIMATION_DURATION = 180;
    public static final int PAGE_BOTTOM = 1;
    public static final int PAGE_TOP = 0;
    public static final double PERCENT = 0.4d;
    public static final String TAG = StickyScrollView.class.getSimpleName();
    public static final int TOUCH_DURATION = 150;
    private int LastVisibility = 0;
    private int currentPage;
    private int deviationHeight;
    private long downTime;
    private int downX;
    private int downY;
    private boolean isFirst = true;
    private boolean isFirstTouch = true;
    private boolean isFixed;
    private boolean isHorizontal;
    private boolean isPageChange;
    private boolean isScrollAuto;
    private boolean isTouch;
    private ViewGroup mBottomChildView;
    private ViewGroup mCardView;
    private ViewGroup mChildLayout;
    private Context mContext;
    private View mFillBottomChildView;
    private Scroller mScroller;
    private View mTopChildView;
    private int offsetDistance;
    private OnPageChangeListener onPageChangeListener;
    private int screenHeight;
    private int topChildHeight;
    private long upTime;
    private int upY;

    public interface OnPageChangeListener {
        void OnPageChange(int i);
    }

    public StickyScrollView(Context context) {
        super(context);
        this.mContext = context;
    }

    public StickyScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
    }

    public StickyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
    }

    public void setOnPageChangeListener(OnPageChangeListener onPageChangeListener2) {
        this.onPageChangeListener = onPageChangeListener2;
    }

    /* access modifiers changed from: protected */
    public void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        this.mChildLayout = (ViewGroup) getChildAt(0);
        this.mTopChildView = this.mChildLayout.getChildAt(0);
        this.mBottomChildView = (ViewGroup) this.mChildLayout.getChildAt(1);
        this.mFillBottomChildView = this.mChildLayout.getChildAt(2);
        this.topChildHeight = this.mTopChildView.getMeasuredHeight();
        this.screenHeight = getMeasuredHeight();
        this.offsetDistance = this.topChildHeight - this.screenHeight;
        this.mCardView = (ViewGroup) this.mBottomChildView.getChildAt(0);
    }

    /* access modifiers changed from: protected */
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (this.isFirst || this.mCardView.getVisibility() != this.LastVisibility) {
            this.isFirst = false;
            this.LastVisibility = this.mCardView.getVisibility();
            if (this.mCardView.getVisibility() == VISIBLE) {
                int cardHight = NiceUtil.dp2px(this.mContext, 470.0f);
                if (this.screenHeight - cardHight > 80) {
                    this.deviationHeight = (this.screenHeight - cardHight) - 60;
                } else {
                    this.deviationHeight = 0;
                }
                this.mBottomChildView.setLayoutParams(new LayoutParams(getMeasuredWidth(), getMeasuredHeight() - this.deviationHeight));
                this.mFillBottomChildView.setLayoutParams(new LayoutParams(getMeasuredWidth(), this.deviationHeight));
                return;
            }
            this.mBottomChildView.setLayoutParams(new LayoutParams(getMeasuredWidth(), getMeasuredHeight() - this.topChildHeight));
            this.mFillBottomChildView.setLayoutParams(new LayoutParams(0, 0));
        }
    }

    public boolean onTouchEvent(MotionEvent ev) {
        if (this.isHorizontal) {
            return false;
        }
        return super.onTouchEvent(ev);
    }

    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (this.isFixed) {
            return super.dispatchTouchEvent(ev);
        }
        switch (ev.getAction()) {
            case 0:
                this.isTouch = true;
                this.isFirstTouch = true;
                this.downY = (int) ev.getY();
                this.downX = (int) ev.getX();
                this.downTime = System.currentTimeMillis();
                if (this.mScroller != null) {
                    this.mScroller.forceFinished(true);
                    this.mScroller = null;
                    break;
                }
                break;
            case 1:
                this.isTouch = false;
                if (!this.isHorizontal) {
                    this.upY = (int) ev.getY();
                    this.upTime = System.currentTimeMillis();
                    long duration = this.upTime - this.downTime;
                    if (Math.abs(this.upY - this.downY) > 50) {
                        Log.e(TAG, ">>>ISN_T CLICK:" + Math.abs(this.upY - this.downY));
                        if (this.currentPage == 0) {
                            if (getScrollY() == 0) {
                                this.mScroller = new Scroller(this.mContext);
                                this.mScroller.startScroll(0, 0, 0, 100, ANIMATION_DURATION);
                                break;
                            } else if (getScrollY() > this.offsetDistance) {
                                this.mScroller = new Scroller(this.mContext);
                                if (((double) getScrollY()) >= ((double) this.topChildHeight) - (((double) this.topChildHeight) * 0.4d) || duration <= 150) {
                                    this.isPageChange = true;
                                    this.isScrollAuto = duration < 150;
                                    scrollToTarget(1);
                                    this.currentPage = 1;
                                    float curTranslationY = this.mBottomChildView.getTranslationY();
                                    ObjectAnimator animator = ObjectAnimator.ofFloat(this.mBottomChildView, "translationY", new float[]{curTranslationY, ((float) (this.deviationHeight / 2)) + curTranslationY});
                                    animator.setDuration(500);
                                    animator.start();
                                } else {
                                    this.isPageChange = false;
                                    scrollToTarget(0);
                                }
                                return false;
                            }
                        } else if (getScrollY() < this.topChildHeight) {
                            this.mScroller = new Scroller(this.mContext);
                            if (((double) getScrollY()) < ((double) this.topChildHeight) - (((double) this.topChildHeight) * 0.4d) || duration < 150) {
                                this.isPageChange = true;
                                this.isScrollAuto = duration < 150;
                                scrollToTarget(0);
                                this.currentPage = 0;
                                float curTranslationY2 = this.mBottomChildView.getTranslationY();
                                ObjectAnimator animator2 = ObjectAnimator.ofFloat(this.mBottomChildView, "translationY", new float[]{curTranslationY2, curTranslationY2 - ((float) (this.deviationHeight / 2))});
                                animator2.setDuration(500);
                                animator2.start();
                            } else {
                                this.isPageChange = false;
                                scrollToTarget(1);
                            }
                            return false;
                        }
                    }
                }
                break;
            case 2:
                float distanceY = ev.getY() - ((float) this.downY);
                float distanceX = ev.getX() - ((float) this.downX);
                if (this.isFirstTouch) {
                    this.isFirstTouch = false;
                    if (Math.abs(distanceX) <= Math.abs(distanceY)) {
                        this.isHorizontal = false;
                        break;
                    } else {
                        this.isHorizontal = true;
                        break;
                    }
                }
                break;
        }
        return super.dispatchTouchEvent(ev);
    }

    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case 2:
                if (this.isHorizontal) {
                    return false;
                }
                break;
        }
        return super.onInterceptTouchEvent(ev);
    }

    private void scrollToTarget(int currentPage2) {
        int i = ANIMATION_DURATION;
        if (currentPage2 == 0) {
            int delta = getScrollY() - this.offsetDistance;
            Scroller scroller = this.mScroller;
            int scrollY = getScrollY();
            int i2 = -delta;
            if (!this.isScrollAuto) {
                i = (int) (((double) Math.abs(delta)) * 0.4d);
            }
            scroller.startScroll(0, scrollY, 0, i2, i);
        } else if (currentPage2 == 1) {
            int delta2 = getScrollY() - this.topChildHeight;
            Scroller scroller2 = this.mScroller;
            int scrollY2 = getScrollY();
            int i3 = -delta2;
            if (!this.isScrollAuto) {
                i = (int) (((double) Math.abs(delta2)) * 0.4d);
            }
            scroller2.startScroll(0, scrollY2, 0, i3, i);
        }
        postInvalidate();
    }

    public void computeScroll() {
        super.computeScroll();
        if (this.mScroller != null && this.mScroller.computeScrollOffset()) {
            scrollTo(this.mScroller.getCurrX(), this.mScroller.getCurrY());
            postInvalidate();
            if (this.mScroller.isFinished()) {
                this.mScroller = null;
                if (this.onPageChangeListener != null && this.isPageChange) {
                    this.onPageChangeListener.OnPageChange(this.currentPage);
                }
            }
        }
    }

    /* access modifiers changed from: protected */
    public void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (this.currentPage == 0) {
            if (getScrollY() > this.offsetDistance && !this.isTouch) {
                if (this.mScroller == null) {
                    scrollTo(0, this.offsetDistance);
                } else {
                    scrollToTarget(0);
                }
            }
        } else if (this.currentPage == 1 && getScrollY() < this.topChildHeight && !this.isTouch) {
            if (this.mScroller == null) {
                scrollTo(0, this.topChildHeight);
            } else {
                scrollToTarget(1);
            }
        }
    }

    public void setFixed(boolean isFixed2) {
        this.isFixed = isFixed2;
        if (isFixed2 && this.currentPage == 1) {
            this.mScroller = new Scroller(this.mContext);
            this.isScrollAuto = true;
            this.isPageChange = true;
            scrollToTarget(0);
            this.currentPage = 0;
            float curTranslationY = this.mBottomChildView.getTranslationY();
            ObjectAnimator animator = ObjectAnimator.ofFloat(this.mBottomChildView, "translationY", new float[]{curTranslationY, curTranslationY - ((float) (this.deviationHeight / 2))});
            animator.setDuration(0);
            animator.start();
        }
    }
}